package com.dmall.promotion.controller;

import com.dmall.promotion.utils.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author: xiao1.wang
 * @date: 2021/7/4 21:05
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @GetMapping("/thread")
    public Result thread() {
        printAllThreadDetail();
        Result result = new Result();
        result.setCode("0000");
        result.setMessage("操作成功");
        return result;
    }

    private void printAllThreadDetail() {
        Map<Thread, StackTraceElement[]> allStackTraces = Thread.getAllStackTraces();
        for (Map.Entry<Thread, StackTraceElement[]> entry : allStackTraces.entrySet()) {
            Thread thread = entry.getKey();
            StackTraceElement[] stack = entry.getValue();
            if (thread.equals(Thread.currentThread())) {
                continue;
            }
            System.out.println("\n线程：" + thread.getName() + "\n");
            for (StackTraceElement element : stack) {
                System.out.println("\t" + element + "\n");
            }
        }
    }
}
